package com.cxrj.zhsq.housePropertyManage.service.impl;


import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.cxrj.zhsq.housePropertyManage.dao.PropertyHouseMapper;
import com.cxrj.zhsq.housePropertyManage.dao.PropertyOwnerMapper;
import com.cxrj.zhsq.housePropertyManage.entity.PropertyOwner;
import com.cxrj.zhsq.housePropertyManage.service.PropertyOwnerService;

@Service
public class PropertyOwnerServiceImpl implements PropertyOwnerService {
	@Resource
	private PropertyOwnerMapper propertyOwnerMapper;
	@Resource
	private PropertyHouseMapper propertyHouseMapper;
	@Override
	public Map<String, Object> insertOwner(PropertyOwner propertyOwner, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		propertyOwner.setId(UUID.randomUUID().toString());
		propertyOwner.setCreateTime(new Date());
		propertyOwner.setType("1");
		propertyOwner.setCommunityId((String)request.getSession().getAttribute("communityId"));
		propertyOwner.setCommunityName((String)request.getSession().getAttribute("communityName"));
		int a=propertyOwnerMapper.insertSelective(propertyOwner);
		sqlMap.put("numberPeriods", propertyOwner.getNumberPeriods());
		sqlMap.put("building", propertyOwner.getBuilding());
		sqlMap.put("unit", propertyOwner.getUnit());
		sqlMap.put("doorCard", propertyOwner.getDoorCard());
		sqlMap.put("state", "1");
		propertyHouseMapper.updateStateById(sqlMap);
		resultMap.put("result", 0);
		if(a>0){
			resultMap.put("result", a);
		}
		return resultMap;
	}
	@Override
	public Map<String, Object> selectOwnerList(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		sqlMap.put("name", request.getParameter("name"));
		sqlMap.put("numberPeriods", request.getParameter("numberPeriods"));
		sqlMap.put("building", request.getParameter("building"));
		sqlMap.put("unit", request.getParameter("unit"));
		sqlMap.put("doorCard", request.getParameter("doorCard"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = propertyOwnerMapper.selectByCount(sqlMap) + "";
		List<PropertyOwner> list = propertyOwnerMapper.selectOwnerList(sqlMap);
		for(int i=0;i<list.size();i++) {
			list.get(i).setIdCard(list.get(i).getIdCard().replaceAll("(\\d{4})\\d{10}(\\w{4})","$1****$2"));
		}

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);
		return resultMap;
	}
	@Override
	public Map<String, Object> deleteOwner(PropertyOwner propertyOwner) {
		Map<String, Object> resultMap=new HashMap<>();
		resultMap.put("result", 0);
		int a=propertyOwnerMapper.updateByPrimaryKeySelective(propertyOwner);
		if (a>0) {
			resultMap.put("result", a);
		}
		return resultMap;
	}
	@Override
	public Map<String, Object> selectOwnerDetails(String id) {
		Map<String, Object> resultMap=new HashMap<>();
		PropertyOwner propertyOwner=propertyOwnerMapper.selectByPrimaryKey(id);
		propertyOwner.setIdCard(propertyOwner.getIdCard().replaceAll("(\\d{4})\\d{10}(\\w{4})","$1****$2"));
		resultMap.put("owner", propertyOwner);
		return resultMap;
	}
	@Override
	public Map<String, Object> updateOwner(PropertyOwner propertyOwner,HttpServletRequest request) {
		Map<String, Object> resultMap=new HashMap<>();
		resultMap.put("result", 0);
		int a=propertyOwnerMapper.updateByPrimaryKeySelective(propertyOwner);
		if (a>0) {
			resultMap.put("result", a);
		}
		return resultMap;
	}
	@Override
	public Map<String, Object> insertOwnerList(MultipartFile file, HttpServletRequest request) throws IOException {
		Map<String, Object> resultMap=new HashMap<>();
		List<PropertyOwner> list = new ArrayList<PropertyOwner>();
		
		Workbook workbook =  new XSSFWorkbook(file.getInputStream());
 
		//获取第一个工作表
		XSSFSheet sheet = (XSSFSheet) workbook.getSheetAt(0);
		
		//获取sheet中第一行行号
		int firstRowNum = sheet.getFirstRowNum();
		//获取sheet中最后一行行号
		int lastRowNum = sheet.getLastRowNum();
		resultMap.put("result", 0);
		try {
			//循环插入数据
			for(int i=firstRowNum+1;i<=lastRowNum;i++){
				XSSFRow row = sheet.getRow(i);
				
				PropertyOwner propertyOwner = new PropertyOwner();
				
				propertyOwner.setId(UUID.randomUUID().toString());
				propertyOwner.setCreateTime(new Date());
				propertyOwner.setType("1");
				propertyOwner.setCommunityId((String)request.getSession().getAttribute("communityId"));
				propertyOwner.setCommunityName((String)request.getSession().getAttribute("communityName"));
				
				XSSFCell name = row.getCell(0);//姓名
				if(name!=null){
					name.setCellType(Cell.CELL_TYPE_STRING);
					propertyOwner.setName((name.getStringCellValue()));
				}
				
				XSSFCell gender = row.getCell(1);//性别
				if(gender!=null){
					gender.setCellType(Cell.CELL_TYPE_STRING);
					propertyOwner.setGender((gender.getStringCellValue()));
				}
				
				XSSFCell idCard = row.getCell(2);//身份证号
				if(idCard!=null){
					idCard.setCellType(Cell.CELL_TYPE_STRING);
					propertyOwner.setIdCard((idCard.getStringCellValue()));
				}
				
				XSSFCell tel = row.getCell(3);//电话
				if(tel!=null){
					tel.setCellType(Cell.CELL_TYPE_STRING);
					propertyOwner.setTel((tel.getStringCellValue()));
				}
				
				XSSFCell emergencyContact = row.getCell(4);//紧急联系人
				if(emergencyContact!=null){
					emergencyContact.setCellType(Cell.CELL_TYPE_STRING);
					propertyOwner.setEmergencyContact((emergencyContact.getStringCellValue()));
				}
				
				XSSFCell emergencyContactTel = row.getCell(5);//紧急联系人电话
				if(emergencyContactTel!=null){
					emergencyContactTel.setCellType(Cell.CELL_TYPE_STRING);
					propertyOwner.setEmergencyContactTel((emergencyContactTel.getStringCellValue()));
				}
				
				XSSFCell numberPeriods = row.getCell(6);//期数
				if(numberPeriods!=null){
					numberPeriods.setCellType(Cell.CELL_TYPE_STRING);
					propertyOwner.setNumberPeriods((numberPeriods.getStringCellValue()));
				}
				
				XSSFCell building = row.getCell(7);//楼栋
				if(building!=null){
					building.setCellType(Cell.CELL_TYPE_STRING);
					propertyOwner.setBuilding((building.getStringCellValue()));
				}
				
				XSSFCell unit = row.getCell(8);//单元
				if(unit!=null){
					unit.setCellType(Cell.CELL_TYPE_STRING);
					propertyOwner.setUnit((unit.getStringCellValue()));
				}
				
				XSSFCell doorCard = row.getCell(9);//门牌
				if(doorCard!=null){
					doorCard.setCellType(Cell.CELL_TYPE_STRING);
					propertyOwner.setDoorCard((doorCard.getStringCellValue()));
				}
				
				XSSFCell s1 = row.getCell(10);//物业缴费时间
				/*if(s1!=null){
					//s1.setCellType(Cell.CELL_TYPE_STRING);
					if (DateUtil.isCellDateFormatted(s1)) {
						 Date d = s1.getDateCellValue();
					     DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
					     propertyOwner.setS1(formater.format(d));
					}
				}*/
				
				list.add(propertyOwner);
			}
			int a=propertyOwnerMapper.insertOwnerList(list);//往数据库插入数据
			int b=propertyHouseMapper.updateStateByHouse(list);
			if(a>0 && b>0){
				resultMap.put("result", 1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", 0);
		}
		return resultMap;
	}
	

}
